package com.truyen.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.truyen.persistence.dao.ChapterTmpTruyenMaDao;
import com.truyen.persistence.entity.ChapterTmpTruyenMa;
import com.truyen.persistence.entity.TruyenMa;
import com.truyen.service.ChapterTmpTruyenMaService;
import com.truyen.utils.PagingInfo;


@Service
public class ChapterTmpTruyenMaServiceImpl implements ChapterTmpTruyenMaService {
	
	@Autowired
	private ChapterTmpTruyenMaDao chapterTmpTruyenMaDao;
	
	@Override
	public void save(ChapterTmpTruyenMa entity) {
		chapterTmpTruyenMaDao.save(entity);
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenMaByIdCacheDataController")
	public ChapterTmpTruyenMa findById(Long id) {
		return chapterTmpTruyenMaDao.findById(id);
	}

	@Override
	public List<ChapterTmpTruyenMa> getAll() {
		return chapterTmpTruyenMaDao.findAll();
	}

	@Override
	public ChapterTmpTruyenMa findByProperty(String column, Object value) {
		List<ChapterTmpTruyenMa> lst = chapterTmpTruyenMaDao.findByQuery(column, value);
		return (lst.size() > 0)?lst.get(0):null;
	}

	@Override
	public List<ChapterTmpTruyenMa> getLstChapterByTruyen(TruyenMa entity, String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTmpTruyenMaDao.getLstChapterByTruyen(entity,kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public ChapterTmpTruyenMa findAliasvTruyen(TruyenMa entity, String alias) {
		return chapterTmpTruyenMaDao.findAliasvTruyen(entity, alias);
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenMaAllCacheDataController")
	public List<ChapterTmpTruyenMa> getLstAllChapterTmpTruyenMa(String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTmpTruyenMaDao.getLstAllChapterTmpTruyenMa(kwSearch, sort, sortBy, pInfo);
	}

	@Override
	public void updateChapterNumber(Long id, float chapterNumberChange) {
		try{
			ChapterTmpTruyenMa chapter = findById(id);
			chapter.setChapterNumber(chapterNumberChange);
			chapterTmpTruyenMaDao.merge(chapter);
		}catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenMaCacheTruyenController")
	public List<ChapterTmpTruyenMa> getByTruyen(TruyenMa entity) {
		return chapterTmpTruyenMaDao.getByTruyen(entity);
	}

	@Override
	public ChapterTmpTruyenMa getChapterByTruyenChapnumber(TruyenMa entity, float chapterNumber) {
		return chapterTmpTruyenMaDao.getChapterByTruyenChapnumber(entity,chapterNumber);
	}

	@Override
	@Transactional
	public void delete(long id) {
		try{
			ChapterTmpTruyenMa chapter = findById(id);
			if(chapter != null){
				chapterTmpTruyenMaDao.delete(chapter);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenMaMoiCapNhatCacheTruyenController")
	public List<ChapterTmpTruyenMa> getListNewChapter(int maxLimit) {
		return chapterTmpTruyenMaDao.getListNewChapter(maxLimit);
	}
	
	@Override
	public void update(Long id, Float chapterNumber) {
		try{
			ChapterTmpTruyenMa chapter = findById(id);
			if(chapter != null){
				chapter.setChapterNumber(chapterNumber);
				chapterTmpTruyenMaDao.merge(chapter);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public List<ChapterTmpTruyenMa> getLstByOrderBy(String columnOrderBy, PagingInfo pInfo) {
		return chapterTmpTruyenMaDao.getLstByOrderBy(columnOrderBy, pInfo);
	}

	@Override
	public List<ChapterTmpTruyenMa> getByTruyenPaging(TruyenMa entity, PagingInfo pInfoChapter) {
		return chapterTmpTruyenMaDao.getByTruyenPaging(entity,pInfoChapter);
	}

	@Override
	public ChapterTmpTruyenMa findByTruyenChapterNumber(TruyenMa entity, float chapterNumber) {
		return chapterTmpTruyenMaDao.findByTruyenChapterNumber(entity,chapterNumber);
	}

	@Override
	public List<Object> getByIdTruyen(Long idTruyen) {
		return chapterTmpTruyenMaDao.getByIdTruyen(idTruyen);
	}

}
